16. Check HTTP Response Code in Soonami App
Check HTTP Response Code in Soonami App
Question:
In the last quiz, you confirmed that a successful request returns a 200 response code.
Let’s build that logic into our app. We should check that we do get at 200 response code, before we continue with trying to parse the JSON response.
If we received an error response code, just return an empty string for the JSON response. it wouldn’t make sense to try to extract the feature array from the response because there would be no earthquake information in the response!
Also double check the code to make sure you handle empty or null cases in the networking code in a reasonable manner. See the note below the quiz for more details. For example, if the url is null, we shouldn’t try to make the HTTP request. Or if the JSON response is null or empty string, we shouldn’t try to continue with parsing it.
Start Quiz:
Solution:
INSTRUCTOR NOTE:
Hint: See HttpUrlConnection getResponseCode() method
To make the code more robust, handle the empty or null case in the networking code. For example, if the url is null, we shouldn’t try to make the HTTP request. Or if the JSON response is null or empty string, we shouldn’t try to continue with parsing it.